package org.example.settings.controller;

import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.Clue;
import org.example.settings.domain.DictionaryValue;
import org.example.settings.domain.User;
import org.example.settings.service.ClueService;
import org.example.settings.service.DictionaryValueService;
import org.example.workbench.domain.ClueActivityRelation;
import org.example.workbench.domain.ClueRemark;
import org.example.workbench.domain.activity;
import org.example.workbench.service.ActivityService;
import org.example.workbench.service.ClueActivityRelationService;
import org.example.workbench.service.ClueRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private ClueRemarkService clueRemarkService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ClueActivityRelationService clueActivityRelationService;
    @Autowired
    private DictionaryValueService dictionaryValueService;

    @RequestMapping("/deleteClueByClueId")
    public @ResponseBody Object deleteClueByClueId(String[] id){
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            for (String clueId : id) {
                clueService.deleteClueByClueId(clueId);
            }
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后......");
        }
        return errorInfo;
    }
    @RequestMapping("/updateClueByMap")
    public @ResponseBody Object updateClueByMap(@RequestParam Map<String,Object> map,HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        map.put("editBy",user.getId());
        map.put("editTime",DateFormatUtils.formatDateTime(new Date()));
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = clueService.updateClueByMap(map);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后......");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后......");
        }
        return errorInfo;
    }
    @RequestMapping("/getClueById")
    public @ResponseBody Object getClueById(String id){
        return clueService.getClueDetailById(id);
    }
    @RequestMapping("/getAllClueByMap")
    public @ResponseBody Object getAllClueByMap(@RequestParam Map<String,Object> map){
        int currentPage = Integer.parseInt((String) map.get("currentPage"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        int pageNo=(currentPage-1)*pageSize;
        map.put("pageSize",pageSize);
        map.put("pageNo",pageNo);
        int totalCount = clueService.getTotalCountClue();
        List<Clue> clueList = clueService.getAllClue(map);
        HashMap<String, Object> newMap = new HashMap<>();
        newMap.put("totalCount",totalCount);
        newMap.put("clueList",clueList);
        return newMap;
    }
    @RequestMapping("/saveConvert")
    @ResponseBody
    public Object saveConvert(String clueId,String money,String name,String expectedDate,String stage,String source,String activityId,String isChecked,HttpSession session){
        HashMap<String, Object> map = new HashMap<>();
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        map.put("clueId",clueId);
        map.put("money",money);
        map.put("name",name);
        map.put("expectedDate",expectedDate);
        map.put("stage",stage);
        map.put("source",source);
        map.put("activityId",activityId);
        map.put("isChecked",isChecked);
        map.put(Constant.SESSION_USER,user);
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            clueService.saveCustomerFromDetail(map);
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙,请稍后.....");
        }
        return errorInfo;
    }
    @RequestMapping("/getActivityByClueIdAndNameToConvert")
    @ResponseBody
    public Object getActivityByClueIdAndNameToConvert(String activityName,String clueId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        return activityService.getActivityByClueIdAndNameToConvert(map);
    }
    @RequestMapping("/getClueIntoConvert")
    public String getClueIntoConvert(String id,HttpServletRequest request){
        Clue clue = clueService.getClueById(id);
        List<DictionaryValue> dictionaryValues = dictionaryValueService.selectDicValueByType("stage");
        request.setAttribute("clue",clue);
        request.setAttribute("dictionaryValues",dictionaryValues);
        return "workbench/clue/convert";
    }
    @RequestMapping("/deleteRelation")
    @ResponseBody
    public Object deleteRelation(ClueActivityRelation relation){
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = clueActivityRelationService.deleteRelationByActivityIdAndClueId(relation);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙,请稍后.....");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙,请稍后.....");
        }
        return errorInfo;
    }
    @RequestMapping("/insertRelations")
    @ResponseBody
    public Object insertRelations(String[] id,String clueId){
        ArrayList<ClueActivityRelation> relationArrayList = new ArrayList<>();
        ClueActivityRelation clueActivityRelation = null;
        for (String s : id) {
            clueActivityRelation = new ClueActivityRelation();
            clueActivityRelation.setActivityId(s);
            clueActivityRelation.setClueId(clueId);
            clueActivityRelation.setId(UUIDUtils.getUUID());
            relationArrayList.add(clueActivityRelation);
        }
        ErrorInfo errorInfo = new ErrorInfo();
        List<activity> activityList = activityService.selectSomeActivityIntoExcel(id);
        try {
            int i = clueActivityRelationService.insertRelations(relationArrayList);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙,请稍后.....");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙,请稍后.....");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityList",activityList);
        map.put("errorInfo",errorInfo);
        return map;
    }
    @RequestMapping("/getActivityByNameAndClueId")
    @ResponseBody
    public Object getActivityByNameAndClueId(String activityName,String clueId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityName",activityName);
        map.put("clueId",clueId);
        return activityService.getActivityByClueIdAndName(map);
    }
    @RequestMapping("/getClueDetails")
    public String getClueDetails(String id, HttpServletRequest request){

        Clue clue = clueService.getClueById(id);
        List<ClueRemark> clueRemarkId = clueRemarkService.getAllClueRemarkById(id);
        List<activity> activityList = activityService.getActivityByClueId(id);
        request.setAttribute("clue",clue);
        request.setAttribute("clueRemarkId",clueRemarkId);
        request.setAttribute("activityByClue",activityList);

        return "workbench/clue/detail";
    }
    @RequestMapping("/insertOneByClue")
    @ResponseBody
    public Object insertOneByClue(Clue clue, HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        clue.setCreateBy(user.getId());
        clue.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        clue.setId(UUIDUtils.getUUID());
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = clueService.insertOneClueByClue(clue);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙,请稍后.....");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙,请稍后.....");
        }
        return errorInfo;
    }
}
